package com.v1.video.cibn;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.starschina.sdk.abs.media.ThinkoPlayerCtrlView;
import com.starschina.sdk.player.ThinkoPlayerView;
import com.starschina.types.DChannel;
import com.v1.video.R;
import org.greenrobot.eventbus.EventBus;

public class VideoCtrlView extends ThinkoPlayerCtrlView {

	private static final String TAG = "VideoCtrlView";
	private Context mContext;

	private Handler mVideoSeekHandler = null;

	private TextView mTitleView;
	private ImageView mZoom;
	private TextView mToLive;
	private TextView mStartPos;
	private TextView mEndPos;
	private ImageView mPauseBt;
	private SeekBar mVideoSeekbar;
	private TextView mVideoReview;

	private int mVideoSeekProgress;
	private long mStartTime;
	private long mEndTime;

	private ThinkoPlayerView mPlayerView;

	private DChannel mChannel;

	private boolean isFullScreen = false;

	public VideoCtrlView(Context act, ThinkoPlayerView playerView) {
		super(act);
		mContext = act;
		mVideoSeekHandler = new Handler();

		mPlayerView = playerView;
		initVideoCtrlView();
	}

	private void initVideoCtrlView() {
		View layout = View.inflate(mContext, R.layout.layout_videoctrl, this);
		mTitleView = (TextView) layout.findViewById(R.id.title);
		mZoom = (ImageView) layout.findViewById(R.id.zoom);
		mToLive = (TextView) layout.findViewById(R.id.tolive);
		mStartPos = (TextView) layout.findViewById(R.id.start_pos);
		mEndPos = (TextView) layout.findViewById(R.id.end_pos);
		mPauseBt = (ImageView) layout.findViewById(R.id.play_pause);
		mVideoSeekbar = (SeekBar) layout.findViewById(R.id.seekbar_video);
		// mVideoReview = (TextView) layout.findViewById(R.id.review);
		mVideoSeekbar.setOnSeekBarChangeListener(mViedoSeekBarListener);

		findViewById(R.id.iv_result).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				EventBus.getDefault().post(
						new SimpleEvent(EventConst.EVENT_EXIT));
			}
		});

		mZoom.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (isFullScreen) {
					isFullScreen = false;
					mZoom.setImageResource(R.drawable.ic_zoon_in);
					EventBus.getDefault().post(
							new SimpleEvent(EventConst.EVENT_PORTRAIT));
				} else {
					isFullScreen = true;
					mZoom.setImageResource(R.drawable.ic_zoon_in);
					EventBus.getDefault().post(
							new SimpleEvent(EventConst.EVENT_LANDSCAPE));
				}
			}
		});

		mToLive.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mToLive.setVisibility(GONE);
				EventBus.getDefault().post(
						new SimpleEvent(EventConst.EVENT_TO_LIVE));
			}
		});

//		mVideoReview.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				EventBus.getDefault().post(
//						new SimpleEvent(EventConst.EVENT_SHOW_LIVE_REVIEW));
//			}
//		});

		mPauseBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (mPlayerView.isPlaying()) {
					mPlayerView.pause();
					refreshPlayerStatus(false);
				} else {
					mPlayerView.start();
					refreshPlayerStatus(true);
				}
			}
		});

	}

	public void setTitle(String title) {
		mTitleView.setText(title);
	}

	public void setChannel(DChannel channel) {
		mChannel = channel;
	}
	

	public void setEpgTime(long startTime, long endTime) {
		mStartTime = startTime * 1000;
		mEndTime = endTime * 1000;
		// mStartPos.setText(DateUtils.getHourAndMin(0));
		// mEndPos.setText(DateUtils.getHourAndMin(mEndTime-mStartTime));
		mStartPos.setText(formatTime(0));
		mEndPos.setText(formatTime(mEndTime - mStartTime));
		int max = (int) ((mEndTime - mStartTime) / 1000);
		mVideoSeekbar.setMax(max);
		mVideoSeekProgress = 0;

		startPos = 0;
	}

	public void refreshPlayerStatus(boolean isplaying) {
		mVideoSeekHandler.removeCallbacks(mVideoSeekRunnable);
		if (isplaying) {
			if (mPauseBt != null) {
				mPauseBt.setImageResource(R.drawable.btn_play_video_pause);
			}
			// 直播
			if (mChannel.type == 1) {
				mVideoSeekHandler.post(mVideoSeekRunnable);
			} else if (mChannel.type == 0) {// 点播
				mVideoSeekHandler.post(mVodRefreshRunnable);
			}
		} else {
			// 直播
			if (mChannel.type == 1) {
				mVideoSeekHandler.removeCallbacks(mVideoSeekRunnable);
			} else if (mChannel.type == 0) {// 点播
				mVideoSeekHandler.removeCallbacks(mVodRefreshRunnable);
			}
			if (mPauseBt != null) {
				mPauseBt.setImageResource(R.drawable.btn_play_video);
			}
		}
	}

	public void showToLive() {
		mToLive.setVisibility(VISIBLE);
	}

	public void ifShowReviewButton(boolean show) {
		if (show) {
			mVideoReview.setVisibility(VISIBLE);
		} else {
			mVideoReview.setVisibility(GONE);
		}
	}

	public void release() {
		if (mVideoSeekHandler != null) {
			mVideoSeekHandler.removeCallbacksAndMessages(null);
		}
	}

	private SeekBar.OnSeekBarChangeListener mViedoSeekBarListener = new SeekBar.OnSeekBarChangeListener() {
		int seek_pos;

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
			seek_pos = progress;
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			if (mChannel.type == 0) {// 点播
				mVideoSeekHandler.removeCallbacks(mVodRefreshRunnable);
			}
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			if (mChannel.type == 1) {// 直播
				startPos = 0;
				mVideoSeekProgress = seekBar.getProgress();
				long playTime = mStartTime + seek_pos * 1000;
				Log.d("demo", "seekBar.onStopTrackingTouch playTime:"
						+ playTime);
				long seekTime = System.currentTimeMillis() - playTime;
				Log.d("demo", "seekBar.onStopTrackingTouch seekTime:"
						+ seekTime);
				if (seekTime < 0) {
					mVideoSeekbar.setProgress(mVideoSeekProgress);
					Toast.makeText(mContext, "只能回看已播放了的节目!", Toast.LENGTH_SHORT)
							.show();
					return;
				}

				EventBus.getDefault().post(
						new SimpleEvent(EventConst.EVENT_LIVE_SEEK,
								(int) (playTime / 1000)));
				mVideoSeekHandler.removeCallbacks(mVideoSeekRunnable);

			} else if (mChannel.type == 0) {// 点播
				final long newposition = seek_pos * mEndTime / 100L;
				seekTo((int) newposition);
				start();
				mVideoSeekHandler.post(mVodRefreshRunnable);
			}

		}
	};

	long startPos = 0;
	private Runnable mVideoSeekRunnable = new Runnable() {
		@Override
		public void run() {
			mVideoSeekProgress = (int) (mPlayerView.getCurrentPosition() - mStartTime / 1000);
			mVideoSeekbar.setProgress(mVideoSeekProgress);
			mVideoSeekbar.setSecondaryProgress((int) (System
					.currentTimeMillis() / 1000 - mStartTime / 1000));

			if ((long) mPlayerView.getCurrentPosition() * 1000 >= mEndTime) {
				// android.util.Log.d("slamb",
				// "----------controller调用 changeEpg--------------");
				// changeEpg(fingEpgByTime((long)mPlayerView.getCurrentPosition()*1000));
				// EventBus.getDefault().post(new
				// EventMessage<>((long)mPlayerView.getCurrentPosition()*1000,VideoContainerFragment.CURRENT_EPG_CHANGE));
				// EventBus.getDefault().post(new
				// EventMessage(VideoContainerFragment.UPDATE_CURRENT_EPG));
			}

			startPos = startPos > 0 ? startPos + 1000 : ((long) mPlayerView
					.getCurrentPosition() * 1000 - mStartTime);
			if ((long) mPlayerView.getCurrentPosition() * 1000 >= mEndTime) {
				startPos = 0;
				EventBus.getDefault().post(
						new SimpleEvent(EventConst.EVENT_SHOW_NEXT_EPG));
			}
			mStartPos.setText(formatTime(startPos));

			// test
			// if (startPos > 10*1000) {
			// EventBus.getDefault().post(new SimpleEvent(1020));
			// }

			mVideoSeekHandler.postDelayed(this, 1000);
		}
	};

	private Runnable mVodRefreshRunnable = new Runnable() {

		@Override
		public void run() {
			if (mPauseBt != null) {
				if (isPlaying()) {
					mPauseBt.setImageResource(R.drawable.btn_play_video_pause);
				} else {
					mPauseBt.setImageResource(R.drawable.btn_play_video);
				}
			}

			mStartTime = getCurrentPosition();
			mStartPos.setText(formatTime(mStartTime));
			if (mEndTime <= 0) {
				mEndTime = getDuration();
			}
			mEndPos.setText(formatTime(mEndTime));

			if (mStartTime > 0 && mEndTime > 0 && mVideoSeekbar != null) {
				mVideoSeekbar.setProgress((int) (mStartTime * 100 / mEndTime));
			}

			mVideoSeekHandler.postDelayed(this, 1000);
		}
	};

	private String formatTime(long duration) {
		String result = null;
		duration = duration / 1000;
		long h = duration / (60 * 60);
		long m = (duration % 3600) / 60;
		long s = duration % 3600 % 60;

		StringBuilder sb = new StringBuilder();
		String zero = "0";
		if (h > 0) {
			if (h < 10) {
				sb.append(zero).append(Long.toString(h));
			} else {
				sb.append(Long.toString(h));
			}
			sb.append(":");
		}
		if (m < 10) {
			sb.append(zero).append(Long.toString(m));
		} else {
			sb.append(Long.toString(m));
		}
		sb.append(":");
		if (s < 10) {
			sb.append(zero).append(Long.toString(s));
		} else {
			sb.append(Long.toString(s));
		}
		result = sb.toString();

		return result;
	}
}
